package util

import (
	"crypto/md5"
	"fmt"
	"os"
	"path/filepath"
)

func Fatalf(args ...any) {
	fmt.Println(args...)
	os.Exit(0)
}

// GetProjRootDir 获取项目根目录
func GetProjRootDir() (string, error) {
	dir, err := os.Getwd()
	if err != nil {
		return "", err
	}
	for i := 0; i <= 5; i++ {
		if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil {
			return dir, nil
		}
		dir = filepath.Dir(dir)
	}
	return "", nil
}

func MD5(str string) string {
	data := []byte(str) //切片
	has := md5.Sum(data)
	md5str := fmt.Sprintf("%x", has) //将[]byte转成16进制
	return md5str
}
